package com.juma.cartridge.rlock;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class LockHolder {

	private final static ThreadLocal<Map<String,Lock>> lockHolds;
	
	static {
		lockHolds = new ThreadLocal<Map<String,Lock>>();
	}
	
	public static void putLock(String key,Lock lock) {
		Map<String,Lock> lockMap = lockHolds.get();
		if (lockMap==null) {
			lockMap = new ConcurrentHashMap<String, Lock>();
			lockHolds.set(lockMap);
		}
		lockMap.put(key,lock);
	}
	
	public static Lock getLock(String key) {
		Map<String,Lock> lockMap = lockHolds.get();
		if (lockMap==null) {
			lockMap = new ConcurrentHashMap<String, Lock>();
			lockHolds.set(lockMap);
		}
		return lockMap.get(key);
	}
	
	public static Lock removeLock(String key) {
		Map<String,Lock> lockMap = lockHolds.get();
		if(lockMap!=null) {
			return lockMap.remove(key);
		}
		return null;
	}
	
}
